import { Request, handleFileInUploading } from '../../utils/util'
import Notify from '@vant/weapp/notify/notify';
const app = getApp()

// pages/deviceLib/deviceLib.ts
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navBarData: { // 导航栏的数据
            title: '器材库',
            scan: true,
            navigateBack: false
        },
        deviceList: [],
        deviceListSelected: [], // 选择的设备类别列表
        typeList: [], // 所有设备类型,
        active: 0,
        pickerIndex: 0,
        array: ['全部显示', '只显示可借'],
        showOverlay: false,
        deviceSelect: {} ,// 选择的设备(想要借用)
        loading:true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {

    },

    scan(e){
        let res = e.detail
        console.log('扫码结果：',res)
    },

    bindPickerChange(e) {
        let deviceListSelectedTmp = []
        if (e.detail.value == 0) { //全部显示
            deviceListSelectedTmp = this.data.deviceList.filter(obj => obj.devicetype === this.data.typeList[this.data.active])
        } else {
            deviceListSelectedTmp = this.data.deviceList.filter(obj => obj.devicetype === this.data.typeList[this.data.active] && obj.remain != 0)
        }
        this.setData({
            pickerIndex: e.detail.value,
            deviceListSelected: deviceListSelectedTmp
        })

    },

    onChange(event) {
        let deviceListSelectedTmp = []
        if (this.data.pickerIndex == 0) { //全部显示
            deviceListSelectedTmp = this.data.deviceList.filter(obj => obj.devicetype === this.data.typeList[event.detail.name])
        } else {
            deviceListSelectedTmp = this.data.deviceList.filter(obj => obj.devicetype === this.data.typeList[event.detail.name] && obj.remain != 0)
        }
        this.setData({
            deviceListSelected: deviceListSelectedTmp,
            active: event.detail.name
        })
    },
    borrow(e) {
        let device = e.detail
        this.setData({
            deviceSelect: device
        })
        this.onClickShow()
    },

    onClickHide() {
        this.setData({
            showOverlay: false
        })
    },

    onClickShow() {
        this.setData({
            showOverlay: true
        })
    },
    borrowSure(e) {
        let device = e.detail
        if (device.remain != 0) {
            wx.navigateTo({
                url: `/pages/borrow/borrow?id=${device.id}`
            })
        }
    },
    borrowSureFromDetail() {
        let device = this.data.deviceSelect
        if (device.remain != 0) {
            wx.navigateTo({
                url: `/pages/borrow/borrow?id=${device.id}`
            })
        } else {
            Notify({
                color: '#ffffff',
                background: app.globalData.notifyColor,
                message: '已被借光啦',
                duration: 1000,
                top: app.globalData.navBarHeight
            });
        }
    },
    
    borrowDetail(e) {
        console.log(`/pages/borrowDetail/borrowDetail?id=${e.currentTarget.dataset.id}`)
        wx.navigateTo({
            url: `/pages/borrowDetail/borrowDetail?id=${e.currentTarget.dataset.id}`
        })
    },
    scan(){
        let that = this
        wx.scanCode({
            onlyFromCamera: true,// 只允许从相机扫码
            scanType: ['qrCode'],
            success(res) {
                // 扫码成功后  在此处理接下来的逻辑
                that.triggerEvent('scan',res.result)
            }
        })
    },
    newItem(){
        console.log("newitem")
        wx.navigateTo({
            url: "/pages/newItem/newItem"
        })
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady(){
        this.setData({loading:false})
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        Request({
            url: '/getdevice',
        }).then(res => {
            let typeListTmp = [...new Set(res.map(obj => obj.devicetype))]
            let deviceListSelectedTmp = res.filter(obj => obj.devicetype === typeListTmp[this.data.active])
            this.setData({
                deviceList: res,
                typeList: typeListTmp,
                deviceListSelected: deviceListSelectedTmp
            })

        }).catch(error => {
            console.log(error);
        });
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.onShow()
        setTimeout(function () {
            wx.stopPullDownRefresh()
        }, 1000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})